import { createRouter, createWebHistory } from 'vue-router'
import login from '../components/login.vue'
import home from "../components/home.vue"
import welcome from "../components/welcome.vue"
import users from "../components/User/users.vue"
import roles from "../components/Role/roles.vue"
import rights from "../components/Role/rights.vue"
// import categories from "../components/Good/categories.vue"
// import goods from "../components/Good/goods.vue"
//如果路径是/就重定向到login页面，如果就是login页面
const routes=[
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/login',
    component:login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',//先重定向到welcome
    children:[
      {path:'/welcome',component:welcome},
      {path:'/users',component:users},
      {path:'/roles',component: roles},//权限管理下有两个子菜单角色列表，
      {path: '/rights',component: rights}

    ]
  }
]

// ,//权限列表
// {path: '/categories',component: categories},
// {path: '/goods',component: goods}

//下面的routes是routes:routes，es6后键和值同名时，可以只写一个键
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//路由守卫           //3个参数
//没有登录，直接切换路径到welcome就会直接跳转到login页面
router.beforeEach((to,from,next)=>{
  if(to.path==='/login') return next();//如果是登录，不拦截，就往下走next()
  let token=sessionStorage.getItem('token');//在login.vue中存了token
  if(!token) return next('/login');//如果token不存在或没有值，就去登录，

  next();//否则往下走，进入login
})
export default router
